<template>
  <div>
    <div class="swiper-container">
      <div class="swiper-wrapper">
        <div v-for="item in bannerlist" :key="item.id" class="swiper-slide">
          <img class="imgUrl" :src="item.img" alt="" />
        </div>
      </div>
      <div class="swiper-pagination"></div>
      <!--分页器。如果放置在swiper-container外面，需要自定义样式。-->
    </div>
  </div>
</template>

<script>
//引入封装好的接口
import { getBanner } from "../request/api";
//引入swiper.min.js
import "../../node_modules/swiper/js/swiper.min";
//引入核心库
import Swiper from "swiper";
export default {
  data() {
    return {
      bannerlist: [],
    };
  },
  mounted() {
    //轮播图接口
    getBanner()
      .then((res) => {
        console.log(res, "轮播图响应");
        if (res.data.code == 200) {
          this.bannerlist = res.data.list;
        }
      })
      .catch((err) => {
        console.log(err, "数据错误");
      });
  },
  updated() {
    console.log("轮播图");
    new Swiper(".swiper-container", {
      loop: true,
      autoplay: {
        //延迟1.5s
        delay: 1800,
      },
      pagination: {
        el: ".swiper-pagination",
      },
    });
  },
};
</script>

<style scoped>
@import "../../node_modules/swiper/css/swiper.min.css";
.swiper-container {
  width: 100%;
  height: 100%;
}

.swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;

  /* Center slide text vertically */
  display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
}
.imgUrl {
  width: 100%;
  height: 4rem;
}
</style>
